------------------ fgui require start ------------------
------------------  fgui require end  ------------------
local UIGuildPanelSkin = Class("UIGuildPanelSkin");
local Listdetails1 = require("View/UIGuild/Skin/Listdetails1Skin")

function UIGuildPanelSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.c1.onChanged:Add(self.onChange_c1, self);
    self.c2.onChanged:Add(self.onChange_c2, self);
    self.c3.onChanged:Add(self.onChange_c3, self);
    self.btn_blackbackground.onClick:Add(self.onTouch_Btn_blackbackground, self);
    self.btn_return.onClick:Add(self.onTouch_Btn_return, self);
    self.btn_active.onClick:Add(self.onTouch_Btn_active, self);
    self.btn_administration.onClick:Add(self.onTouch_Btn_administration, self);
    self.btn_log.onClick:Add(self.onTouch_Btn_log, self);
    self.btn_Search.onClick:Add(self.onTouch_Btn_search, self);
    --self.list_memberdetails.onClickItem:Add(self.onClickItem_List_memberdetails,self);
    self.list_memberdetails.itemRenderer = function(index, obj)
        self:onRender_List_memberdetailsItem(index, obj);
    end;
    self.AllyStatus.selectedIndex = 0;
    self.guildCtrl.selectedIndex = 0;
    self.btn_cancel.onClick:Add(self.onTouch_Btn_cancel, self);
    self.btn_BuildAlly.onClick:Add(self.onTouch_Btn_BuildAlly, self);
    self.btn_DismissAlly.onClick:Add(self.onTouch_Btn_DismissAlly, self);
    self.btn_change.onClick:Add(self.onTouch_Btn_change, self);
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function UIGuildPanelSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.c1 = com:GetController("c1");
    self.c2 = com:GetController("c2");
    self.c3 = com:GetController("c3");
    self.btn_blackbackground = com:GetChild("Btn_blackbackground");
    self.progressBar_guildexperience = com:GetChild("ProgressBar_guildexperience");
    self.btn_return = com:GetChild("Btn_return");
    self.com_progress = com:GetChild("Com_progress");
    self.btn_active = com:GetChild("Btn_active");
    self.btn_administration = com:GetChild("Btn_Record");
    self.btn_log = com:GetChild("Btn_Journal");
    self.btn_Search = com:GetChild("Btn_Research");
    self.com_blank = com:GetChild("Com_blank");
    self.ldr_flag = com:GetChild("ldr_flag");
    self.list_memberdetails = com:GetChild("List_memberdetails");
    self.text_numberofguildmembers = com:GetChild("Text_numberofguildmembers");
    self.text_lv = com:GetChild("Text_lv");
    self.text_guildcoin = com:GetChild("Text_guildcoin");
    self.text_guildName = com:GetChild("Text_guildName");
    self.text_number = com:GetChild("Text_number");
    self.text_address = com:GetChild("Text_address");
    self.text_leavingamessage = com:GetChild("Text_leavingamessage");
    self.text_administration = com:GetChild("Text_administration");
    self.text_journal = com:GetChild("Text_journal");
    self.text_content1 = com:GetChild("Text_content1");
    self.text_content2 = com:GetChild("Text_content2");
    self.text_content3 = com:GetChild("Text_content3");
    self.text_content4 = com:GetChild("Text_content4");
    self.text_content5 = com:GetChild("Text_content5");
    self.text_content6 = com:GetChild("Text_content6");
    self.text_content7 = com:GetChild("Text_content7");
    self.text_content8 = com:GetChild("Text_content8");
    self.text_content9 = com:GetChild("Text_content9");
    self.text_content10 = com:GetChild("Text_content10");
    self.text_content11 = com:GetChild("Text_content11");
    self.text_content12 = com:GetChild("Text_content12");

        ------ 军团结盟 --------
    self.AllyStatus = com:GetController("AllyStatus");
    self.guildCtrl = com:GetController("guild");
    self.text_AllyName = com:GetChild("Text_AllyName");
    self.text_AllyTime = com:GetChild("Text_AllyTime");
    self.btn_cancel = com:GetChild("Btn_cancel");
    self.btn_BuildAlly = com:GetChild("Btn_BuildAlly");
    self.btn_DismissAlly = com:GetChild("Btn_DismissAlly");
    self.btn_change = com:GetChild("btn_change");

    ------------------  fgui变量绑定end  ------------------
    self.com_blank.onClick:Add(self.onTouch_Com_blank, self);
    self.com_progress.onClick:Add(self.onTouch_Com_progress, self);
    self.ctl_pBar = self.progressBar_guildexperience:GetController("type");
end

function UIGuildPanelSkin:init()
    self.btn_administration.text = getLanguage("GuildInvestment_1057");
    self.btn_log.text = getLanguage("GuildInvestment_1058");
    self.text_leavingamessage.text = getLanguage("GuildInvestment_1097");
    self.text_content1.text = getLanguage("GuildInvestment_1098");
    self.text_content2.text = getLanguage("GuildInvestment_1099");
    self.text_content3.text = getLanguage("GuildInvestment_1100");
    self.text_content4.text = getLanguage("GuildInvestment_1101");
    self.text_content5.text = getLanguage("GuildInvestment_1102");
    self.text_content10.text = getLanguage("GuildInvestment_1106");
    self.text_content9.text = getLanguage("GuildInvestment_1107");
    self.text_content12.text = string.format(getLanguage("GuildInvestment_1111"), 0);
    self.listItems = {}
    ListenEvent(Config.EventType.Union_Info_Change, self.updateView, self);
    ListenEvent(Config.EventType.FreashRoleInfoOther, self.refreshOtherRoleInfo, self);
    ListenEvent(Config.EventType.Union_EventLog_S2C, self.resUnionEventLog, self);
    ListenEvent(Config.EventType.FriendCompeteBattle, self.OpenFriendCompeteBattle, self);
    ListenEvent(Config.EventType.Union_DetailInfo_S2C, self.respAllianceUnionInfo, self);
    ListenEvent(Config.EventType.Union_AllianceExit_S2C, self.respAllianceExit, self);
    ListenEvent(Config.EventType.Union_AllianceExitCancel_S2C, self.respAllianceExitCancel, self);
    ListenEvent(Config.EventType.Union_AllianceInfoList_S2C, self.respAllianceInfoList, self);
    ListenEvent(Config.EventType.Union_AllianceJoin_S2C, self.respAllianceJoin, self);
    ListenEvent(Config.EventType.Union_AllianceJoin_ClosePanel_S2C, self.respAllianceJoinClosePanel, self);
    self.guildCtrl.selectedIndex = 0;
    self.timeCount = -100;
    self.isClickBtn = false;
    self.isClosePanel = false;
    self:hideButton();
    self:updateView();
end

function UIGuildPanelSkin:hideButton()
    self.btn_cancel.visible = false;
    self.btn_BuildAlly.visible = false;
    self.btn_DismissAlly.visible = false;
end

function UIGuildPanelSkin:updateView()
    if self.isClosePanel then
        return;
    end
    if self.guildCtrl.selectedIndex == 0 then
        self.guildMembers = {};
        UIRedDots.removeViewEvent(PanelResNames.UIGuildPanel);
        self:refreshGuildInfo(logicMgr.GuildManager.unionInfo);
    end
end

function UIGuildPanelSkin:refreshGuildInfo(unionInfo)
    self:hideButton();
    if unionInfo then
        self.text_guildName.text = unionInfo.name;
        self.text_number.text = string.format(getLanguage("GuildInvestment_1050"), unionInfo.id);
        self.text_leavingamessage.text = unionInfo.unionOutlineInfo.unionSetting.unionNotice or self.text_leavingamessage.text;
        local guildLv = tonumber(unionInfo.unionOutlineInfo.lv);
        self.text_lv.text = getLanguage("Sys_HeroLevel") .. guildLv;
        self.text_address.text = getLanguage("GuildInvestment_1145") .. logicMgr.GuildManager.getUnionAddress(unionInfo);
        self.ldr_flag.url = logicMgr.GuildManager.getFlagUrl(unionInfo.unionOutlineInfo.unionSetting.flagId);
        self.guildMembers = unionInfo.members;
        local memberCount = #self.guildMembers;
        self.list_memberdetails.numItems = memberCount;
        utimer.delayCall(function ()
            HelperFunc.ListTranstionPlay(self.list_memberdetails)
        end ,100)
        self.text_numberofguildmembers.text = string.format(getLanguage("GuildInvestment_1056"), memberCount, logicMgr.GuildManager.getMaxMemberCnt(guildLv));
        self.progressBar_guildexperience.min = 0;
        self.progressBar_guildexperience.max = logicMgr.GuildManager.getNextLevelExp(guildLv);
        self.progressBar_guildexperience.value = unionInfo.unionOutlineInfo.exp;
        self.btn_active.text = tostring(unionInfo.unionOutlineInfo.liveness);
        self.text_content6.text = string.format(getLanguage("GuildInvestment_1103"), unionInfo.unionOutlineInfo.liveness, logicMgr.GuildManager.getGuildParaIntValue(37));
        local memInfo = logicMgr.GuildManager.getMemberInfoByPlayerId();
        local todayLiveNess = memInfo and memInfo.livenessDay or 0;
        local weekLiveNess = memInfo and memInfo.liveness or 0;
        self.text_content7.text = string.format(getLanguage("GuildInvestment_1103"), todayLiveNess, logicMgr.GuildManager.getGuildParaIntValue(36));
        self.text_content8.text = tostring(weekLiveNess);
        UIRedDots.addViewListen(PanelResNames.UIGuildPanel, self.fguiObj.name .. self.btn_administration.name, self.btn_administration, Config.RedDotsType.GuildApply);
        UIRedDots.addViewListen(PanelResNames.UIGuildPanel, self.fguiObj.name .. self.btn_BuildAlly.name, self.btn_BuildAlly, Config.RedDotsType.GuildAlly);
        if (unionInfo.leader ~= nil) and tostring(unionInfo.leader.showInfo.playerId) == tostring(logicMgr.HeroManager.GetRoleInfo().playerId) then
            --self.btn_cancel.visible = true;
            self.btn_BuildAlly.visible = true;
            --self.btn_DismissAlly.visible = true;
        end
        --self:updateAllianceInfo(unionInfo);  -- 屏蔽结盟信息
    end

    local guildCfg = logicMgr.GuildManager.getCurLevelGuildCfg();
    local str = getLanguage("GuildInvestment_1110");
    self.ctl_pBar.selectedIndex = 1;
    local allGuildCfg = G.dataTable["t_guild"];

    if table.getn(allGuildCfg) ~= GuildManager.getUnionLv() and guildCfg then
        str = string.format(getLanguage("GuildInvestment_1112"), guildCfg.f_VicePresident, guildCfg.f_Number);
        self.ctl_pBar.selectedIndex = 0;
    end
    self.text_content11.text = str;

    local guildCoin = ItemManager.getBagNumByTableID(CurrencyEnum.GuildCoin)
    self.text_guildcoin.text = getNumString(guildCoin);
    self.btn_administration:GetChild("UIRedDots"):GetController("c1").selectedIndex = logicMgr.GuildManager.hasApplyGuildMembers() and 2 or 0;
end

function UIGuildPanelSkin:DestroyTimer()
    if self.dismissTimer ~= nil then
        utimer.remove(self.dismissTimer);
        self.dismissTimer = nil;
    end
end

function UIGuildPanelSkin:respAllianceUnionInfo(msg)
    if msg.unionInfo and self.isClickBtn then
        self.guildMembers = {};
        UIRedDots.removeViewEvent(PanelResNames.UIGuildPanel);
        self:refreshGuildInfo(msg.unionInfo);
        self.guildCtrl.selectedIndex = 1;
        self.isClickBtn = false;
    end
end

function UIGuildPanelSkin:respAllianceExit()
    self:updateAllianceInfo(logicMgr.GuildManager.unionInfo);
    self.isClickBtn = false;
end

function UIGuildPanelSkin:respAllianceExitCancel()
    self:updateAllianceInfo(logicMgr.GuildManager.unionInfo);
    self.timeCount = -100;
    self.isClickBtn = false;
end

function UIGuildPanelSkin:respAllianceInfoList()
    if not isOpenUI(PanelNames.UIGuildAllyPanel) then
        showUI(PanelNames.UIGuildAllyPanel);
    end
    self.isClickBtn = false;
end

function UIGuildPanelSkin:respAllianceJoin(type)
    if tonumber(type) == 1 then
        self:closeGuildPanel();
    end
end

function UIGuildPanelSkin:respAllianceJoinClosePanel(type)
    if tonumber(type) == 1 then
        self.isClosePanel = true;
    end
end

function UIGuildPanelSkin:closeGuildPanel()
    if logicMgr.GuildManager.unionInfo and not getUI(PanelResNames.UIGuild) then
        showUI(PanelNames.UIGuild);
    end
    DestoryUI(PanelNames.UIGuildPanel);
end

function UIGuildPanelSkin:updateAllianceInfo(unionInfo)
    if tonumber(unionInfo.allianceId) > 0 then
        local cancelTime = tonumber(tostring(unionInfo.unionOutlineInfo.cancelTime));
        self:DestroyTimer();
        if cancelTime > 0 then
            local serverTime = HelperFunc.GetServerTime();
            if serverTime > cancelTime then
                self.AllyStatus.selectedIndex = 0;
            else
                self.AllyStatus.selectedIndex = 2;
                if tonumber(unionInfo.unionOutlineInfo.cancel) == 0 then
                    self.btn_cancel.visible = false;
                end
                self.timeCount = cancelTime - serverTime;
                self.text_AllyTime.text = HelperFunc.GetTimeStr(self.timeCount);
                self.dismissTimer = utimer.timeCall(function()
                    self.timeCount = self.timeCount - 1;
                    if self.timeCount < 0 then
                        self:DestroyTimer();
                        self.AllyStatus.selectedIndex = 0;
                    else
                        self.text_AllyTime.text = HelperFunc.GetTimeStr(self.timeCount);
                    end
                end, 1000, -1);
            end
        else
            self.AllyStatus.selectedIndex = 1;
        end
        if tonumber(unionInfo.unionOutlineInfo.allianceType) == 0 then
            self.text_AllyName.text = string.format(getLanguage("GuildAllyName"), unionInfo.name, unionInfo.allianceName);
        end
    else
        self.AllyStatus.selectedIndex = 0;
    end
end

function UIGuildPanelSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.c1.onChanged:Clear();
    self.c2.onChanged:Clear();
    self.c3.onChanged:Clear();
    self.btn_blackbackground.onClick:Clear();
    self.btn_return.onClick:Clear();
    self.btn_active.onClick:Clear();
    self.btn_administration.onClick:Clear();
    self.btn_log.onClick:Clear();
    self.btn_Search.onClick:Clear();
    self.list_memberdetails.onClickItem:Clear();
    self.list_memberdetails.itemRenderer = nil;
    self.btn_cancel.onClick:Clear();
    self.btn_BuildAlly.onClick:Clear();
    self.btn_DismissAlly.onClick:Clear();
    self.btn_change.onClick:Clear();
    ------------------  fgui事件移除end  ------------------
    self.com_blank.onClick:Clear();
    self.com_progress.onClick:Clear();
end

function UIGuildPanelSkin:onTouch_Btn_blackbackground(obj)
end

function UIGuildPanelSkin:onTouch_Btn_return(obj)
end

function UIGuildPanelSkin:onTouch_Btn_administration(obj)
    showUI(PanelNames.UIGuildSetting)
end

--function UIGuildPanelSkin:onClickItem_List_memberdetails(context)
--    local item = self.listItems[context.data];
--    if  tostring(logicMgr.HeroManager.GetRoleInfo().playerId) == tostring(item:getPlayerId()) then
--        -- showUI(PanelNames.UIHeadInfo);
--    else
--        roleInfoHandler.sendreqOtherRoleInfo(item:getPlayerId());
--    end
--end

function UIGuildPanelSkin:onRender_List_memberdetailsItem(index, obj)
    local item = self.listItems[obj];
    if item == nil then
        item = Listdetails1.New(obj);
        item:setTouchBtnActiveCallback(function()
            local memInfo = logicMgr.GuildManager.getMemberInfoByPlayerId(item:getPlayerId());
            local weekLiveNess = memInfo and memInfo.liveness or 0;
            self.text_content12.text = string.format(getLanguage("GuildInvestment_1111"), weekLiveNess);
            self.c3.selectedIndex = 1;
        end);
        self.listItems[obj] = item;
    end
    item.alpha = 0;
    item:onRender(self.guildMembers[index + 1],true);
    if tostring(item:getPlayerId()) ~= logicMgr.HeroManager.GetRoleId() then
        item.btn_FriendCompeteBattle.visible = true;
    else
        item.btn_FriendCompeteBattle.visible = false;
    end
end

function UIGuildPanelSkin:onTouch_Btn_log(obj)
    unionHandler.reqUnionEventLogs();
end

function UIGuildPanelSkin:onTouch_Btn_search(obj)
    unionHandler.ReqApplyUnions()
    unionHandler.reqUnionInfoList();
    showUI(PanelNames.UIGuildRecommond);
end

function UIGuildPanelSkin:onTouch_Btn_cancel(obj)
    if self.isClickBtn then
        return;
    end
    self.isClickBtn = true;
    unionHandler.ReqAllianceExitCancel();
end

function UIGuildPanelSkin:onTouch_Btn_BuildAlly(obj)
    if self.isClickBtn then
        return;
    end
    self.isClickBtn = true;
    --if logicMgr.GuildManager.unionInfo then
    --    local allianceCd = tonumber(tostring(logicMgr.GuildManager.unionInfo.unionOutlineInfo.allianceCd1));
    --    if allianceCd > 0 and HelperFunc.GetServerTime() < allianceCd then
    --        UISysTips.AddMsg(getLanguage("GuildAllyCD"));
    --        self.isClickBtn = false;
    --        return;
    --    end
    --end
    unionHandler.sendReqAllianceInfoList();
end

function UIGuildPanelSkin:onTouch_Btn_DismissAlly(obj)
    if self.isClickBtn then
        return;
    end
    self.isClickBtn = true;
    if logicMgr.GuildManager.unionInfo then
        local allianceCd = tonumber(tostring(logicMgr.GuildManager.unionInfo.unionOutlineInfo.allianceCd2));
        if allianceCd > 0 and HelperFunc.GetServerTime() < allianceCd then
            UISysTips.AddMsg(getLanguage("GuildAllyCD"));
            self.isClickBtn = false;
            return;
        end
    end
    local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
    pop.Init(111, function (flag)
        if flag then
            unionHandler.sendReqAllianceExit();
        else
            self.isClickBtn = false;
        end
    end);
end

function UIGuildPanelSkin:onTouch_Btn_change(obj)
    if self.isClickBtn then
        return;
    end
    self.isClickBtn = true;
    if self.timeCount < 0 and self.timeCount > -100 then
        UISysTips.AddMsg(getLanguage("GuildAllyDismiss"));
        self:closeGuildPanel();
    else
        if self.guildCtrl.selectedIndex == 0 then
            if logicMgr.GuildManager.unionInfo ~= nil then
                local allianceId = tonumber(logicMgr.GuildManager.unionInfo.allianceId);
                if allianceId > 0 then
                    unionHandler.reqUnionByIdInfo(allianceId);
                else
                    self.isClickBtn = false;
                end
            else
                self.isClickBtn = false;
            end
        else
            self.guildCtrl.selectedIndex = 0;
            self:updateView();
            self.isClickBtn = false;
        end
    end
end

function UIGuildPanelSkin:onChange_c1(event)
    self.com_blank.visible = self.c1.selectedIndex == 1;
end

function UIGuildPanelSkin:onChange_c2(event)
    self.com_blank.visible = self.c2.selectedIndex == 1;
end

function UIGuildPanelSkin:onChange_c3(event)
    self.com_blank.visible = self.c3.selectedIndex == 1;
end

function UIGuildPanelSkin:onTouch_Btn_active(obj)
    if self.guildCtrl.selectedIndex == 0 then
        self.c1.selectedIndex = 1;
    end
end

function UIGuildPanelSkin:onTouch_Com_blank(obj)
    self.c1.selectedIndex = 0;
    self.c2.selectedIndex = 0;
    self.c3.selectedIndex = 0;
end

function UIGuildPanelSkin:onTouch_Com_progress(obj)
    self.c2.selectedIndex = 1;
end

------------------  ↑ fgui事件句柄 ↑  ------------------

function UIGuildPanelSkin:setCloseCallback(func, target)
    --self.btn_blackbackground.onClick:Add(func,target);
    self.btn_return.onClick:Add(func, target);
end

function UIGuildPanelSkin:refreshOtherRoleInfo(info)
    showUI(PanelNames.UIPlayerShow, info,nil,false,function()
        UIBottom.enterAdventure()
        showUI(PanelNames.UIGuild);
    end)
end

function UIGuildPanelSkin:resUnionEventLog()
    if logicMgr.GuildManager.unionEventLogs then
        showUI(PanelNames.UIGuildLog);
    end
end

function UIGuildPanelSkin:beforeDestroy()
    self.listItems = {}
    CloseEvent(Config.EventType.Union_Info_Change, self.updateView, self);
    CloseEvent(Config.EventType.FreashRoleInfoOther, self.refreshOtherRoleInfo, self)
    CloseEvent(Config.EventType.Union_EventLog_S2C, self.resUnionEventLog, self);
    CloseEvent(Config.EventType.FriendCompeteBattle, self.OpenFriendCompeteBattle, self);
    CloseEvent(Config.EventType.Union_DetailInfo_S2C, self.respAllianceUnionInfo, self);
    CloseEvent(Config.EventType.Union_AllianceExit_S2C, self.respAllianceExit, self);
    CloseEvent(Config.EventType.Union_AllianceExitCancel_S2C, self.respAllianceExitCancel, self);
    CloseEvent(Config.EventType.Union_AllianceInfoList_S2C, self.respAllianceInfoList, self);
    CloseEvent(Config.EventType.Union_AllianceJoin_S2C, self.respAllianceJoin, self);
    CloseEvent(Config.EventType.Union_AllianceJoin_ClosePanel_S2C, self.respAllianceJoinClosePanel, self);

    UIRedDots.removeViewEvent(PanelResNames.UIGuildPanel);
    self:DestroyTimer();
end

function UIGuildPanelSkin:OpenFriendCompeteBattle()

    local param = {};
    --param.playerId = 0;
    local info = logicMgr.FriendManager.GetFriendCompeteBattleInfo()
    param.power = 0;
    param.supSkillId = info.supportSkill;
    param.wins = info.winTimes;
    param.competeTimes = info.competeTimes;
    --param.type = EnumConst.FormationType.ArenaDef;
    logicMgr.FriendManager.SetFriendCompeteBattleType(EnumConst.FightTypeConst.GuildFriendCompeteBattle)
    showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.GuildFriendCompeteBattle, param, function(formations)

        local flag = true;
        if formations[1] ~= nil then
            for i = 1, #formations[1] do
                if formations[1][i] ~= nil and tonumber(tostring(formations[1][i])) > 0 then
                    flag = false;
                    break ;
                end
            end
        end
        if flag then
            UISysTips.AddMsg("需要上阵至少一名英雄");
        else
            local supSkills = logicMgr.SkillManager.getSupSkillInfo();
            local supSkillId = 0;
            for i = 1, #supSkills do
                if supSkills[i].isUse == 1 then
                    supSkillId = tostring(supSkills[i].skillId);
                end
            end
            local info = logicMgr.FriendManager.GetFriendCompeteBattleInfo()

            friendHandler.ReqFriendCompeteBattle(info.friendId, formations[1], supSkillId)
        end
    end, function()
        --showCtrl.selectedPage = "show";
    end)

end

return UIGuildPanelSkin;
